#!/usr/bin/ruby
require "rubygems"
require "opengl"
require "glut"
require "m3g"
require "pp"
require "values"

class MyGLUTWidget
  def initialize
    GLUT.Init
    GLUT.InitWindowSize 256, 256
    GLUT.InitDisplayMode GLUT::GLUT_DOUBLE | GLUT::GLUT_RGB | GLUT::GLUT_DEPTH
    GLUT.CreateWindow "Ruby-M3G"

    initialize_scene

    GLUT.ReshapeFunc  method(:reshape).to_proc()
    GLUT.DisplayFunc  method(:display).to_proc()
    GLUT.KeyboardFunc method(:keyboard).to_proc()
    GLUT.MouseFunc    method(:mouse).to_proc()
    GLUT.IdleFunc     method(:idle).to_proc()

  end

  def initialize_scene

    controller = M3G::AnimationController.new
    keyframe_light_color = M3G::KeyframeSequence.new 3, 3, M3G::KeyframeSequence::LINEAR

    frame_light_color = [[1,1,0], [0,1,0], [1,1,0], [1,0,0]]
    keyframe_light_color.keyframe[0] = 0, frame_light_color[0]
    keyframe_light_color.keyframe[1] = 100, frame_light_color[1]
    keyframe_light_color.keyframe[2] = 200, frame_light_color[2]
    keyframe_light_color.repeat_mode = M3G::KeyframeSequence::LOOP
    keyframe_light_color.valid_range = 0, 2
    keyframe_light_color.duration    = 200
    
    animation_light_color = M3G::AnimationTrack.new keyframe_light_color, M3G::AnimationTrack::COLOR
    animation_light_color.controller = controller

    positions = M3G::VertexArray.new 840, 3, 2
    normals   = M3G::VertexArray.new 840, 3, 1
    positions.values[0,840] = $position_values
    normals.values[0,840] = $normal_values
    
    scale = 1
    bias  = [0,0,0]
    vertices = M3G::VertexBuffer.new
    vertices.positions = positions, scale, bias
    vertices.normals   = normals
    strips = [42,42,42,42,42,
              42,42,42,42,42,
              42,42,42,42,42,
              42,42,42,42,42]
    tris = M3G::TriangleStripArray.new 0, strips

    mat = M3G::Material.new
    mat.color[M3G::Material::DIFFUSE] = 0x7fffffff

    app = M3G::Appearance.new
    app.material = mat

    mesh = M3G::Mesh.new vertices, tris, app

    cam = M3G::Camera.new
    cam.look_at 0,20,20,
                0,0,0,
                0,1,0

    @lgh = M3G::Light.new
    @lgh.color = 0x7fffff00
    @lgh.intensity = 1
    @lgh.mode = M3G::Light::OMNI
    @lgh.translate 0,2,0
    @lgh.pre_rotate -90, 1,0,0
    @lgh.spot.angle    = 60
    @lgh.spot.exponent = 1
    @lgh.animation_track << animation_light_color
    
    @wld = M3G::World.new
    @wld.active_camera = cam  
    @wld.child << cam
    @wld.child << @lgh
    @wld.child << mesh

    @world_time = 0
  end

  def reshape width, height
    g3d = M3G::Graphics3D::instance
    g3d.viewport = 0,0,width,height
    @wld.active_camera.perspective = 45, width/(height*1.0), 0.1, 100
  end

  def display
    g3d = M3G::Graphics3D::instance
    g3d.render @wld
    GLUT.SwapBuffers()
  end
  
  def keyboard key, x, y
    if key == 113  # 'q'
      exit
    elsif key == 115 # 's'
      @lgh.mode = M3G::Light::SPOT
    elsif key == 111 # 'o'
      @lgh.mode = M3G::Light::OMNI
    elsif key == 100 # 'd'
      @lgh.mode = M3G::Light::DIRECTIONAL
    elsif  key == 32 # ' '
      @wld.animate @world_time
      @world_time += 2
    end
    GLUT.PostRedisplay
  end

  def mouse button, state, x, y
  end

  def idle
   GLUT.PostRedisplay
  end

  def start
    GLUT.MainLoop
  end

end


MyGLUTWidget.new.start

